package com.example.lib_base.base.extensions

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.regex.Pattern

//fun  md5(content: String): String {
//    val hash = MessageDigest.getInstance("MD5").digest(content.toByteArray())
//    val hex = StringBuilder(hash.size * 2)
//    for (b in hash) {
//        var str = Integer.toHexString(b.toInt())
//        if (b < 0x10) {
//            str = "0$str"
//        }
//        hex.append(str.substring(str.length -2))
//    }
//    return hex.toString()
//}


fun String.md5():String{
    try {
        val  instance:MessageDigest = MessageDigest.getInstance("MD5")//获取md5加密对象
        val digest:ByteArray = instance.digest(this.toByteArray())//对字符串加密，返回字节数组
        var sb : StringBuffer = StringBuffer()
        for (b in digest) {
            var i :Int = b.toInt() and 0xff//获取低八位有效值
            var hexString = Integer.toHexString(i)//将整数转化为16进制
            if (hexString.length < 2) {
                hexString = "0" + hexString//如果是一位的话，补0
            }
            sb.append(hexString)
        }
        return sb.toString()

    } catch (e: NoSuchAlgorithmException) {
        e.printStackTrace()
    }

    return ""
}

fun String.isPhoneNum():Boolean{
    val compile = Pattern.compile("^(13|14|15|16|17|18|19)\\d{9}$")
    val matcher = compile.matcher(this)
    return matcher.matches()
}
